package com.jsu.springboot_crop.controller;

import com.github.pagehelper.PageInfo;
import com.jsu.springboot_crop.common.Result;
import com.jsu.springboot_crop.common.StatusCode;
import com.jsu.springboot_crop.entity.Expert;
import com.jsu.springboot_crop.entity.Question;
import com.jsu.springboot_crop.service.ExpertService;
import com.jsu.springboot_crop.service.QuestionService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/question")
public class QuestionController {
    @Autowired
    ExpertService expertService;
    @Autowired
    QuestionService questionService;
    @GetMapping("/findAllQues/{pageNum}")
    public Result<PageInfo<Question>> findAllQues(@PathVariable("pageNum") Integer pageNum){
        PageInfo<Question>questionPageInfo=questionService.selectByKeys("",pageNum);
        return new Result<PageInfo<Question>>(true, StatusCode.OK,"查询成功",questionPageInfo);
    }
    //按条件分页条件查询所有问答情况
    @GetMapping("/findPageQues/{keys}/{pageNum}")
    public Result<PageInfo<Question>> findPageQues(@PathVariable("keys") String keys, @PathVariable("pageNum") Integer pageNum){
        PageInfo<Question>questionPageInfo=questionService.selectByKeys(keys,pageNum);
        return new Result<PageInfo<Question>>(true, StatusCode.OK,"查询成功",questionPageInfo);
    }
    //根据ID查询询问情况
    @ApiOperation(value = "根据ID查询询问情况")
    @GetMapping("/selectId/{id}")
    public Result selectById(@PathVariable("id") Integer id){
        Question question=questionService.selectById(id);
        return new Result(true, StatusCode.OK,"查询成功",question);
    }
    //添加询问情况
    @PostMapping("/add")
    public Result add(@RequestBody Question question, BindingResult bindingResult){
        //检查项目是否有效
        if (bindingResult.hasErrors()) {
            StringBuffer stringBuffer = new StringBuffer();
            List<ObjectError> allErrors = bindingResult.getAllErrors();
            for (ObjectError objectError : allErrors) {
                stringBuffer.append(objectError.getDefaultMessage()).append("; ");
            }
            String s = stringBuffer.toString();
            System.out.println(s);
            return new Result<String>(false, StatusCode.ERROR, "添加失败",s);
        }
        //调用insert方法
        questionService.insert(question);
        return new Result(true, StatusCode.OK,"添加成功");
    }
    @GetMapping("/findExpert/{pageNum}")
    public Result<PageInfo<Expert>> findPage(@PathVariable("pageNum") Integer pageNum){
        PageInfo<Expert>expertPageInfo=expertService.findPage(pageNum);
        return new Result<PageInfo<Expert>>(true, StatusCode.OK,"查询成功",expertPageInfo);
    }
    //分页条件查询专家
    @GetMapping("/findExpertByKeys/{keys}/{pageNum}")
    public Result<PageInfo<Expert>> findExpertByKeys(@PathVariable("keys")String keys, @PathVariable("pageNum")Integer pageNum){
        PageInfo<Expert>expertPageInfo=expertService.findPageByKeys(keys,pageNum);
        return new Result<PageInfo<Expert>>(true, StatusCode.OK,"查询成功",expertPageInfo);
    }

    //根据用户查询询问情况
    @GetMapping("/selectByKind/{kind}")
    public Result findAllQues(@PathVariable("kind")String kind){
        List<Question>questions= questionService.selectByQuestion(kind);
        return new Result(true,StatusCode.OK,"查询成功",questions);
    }

    //根据id修改询问情况
    @PutMapping("/update")
    public Result update(@RequestBody Question question,BindingResult bindingResult){
        //检查项目是否有效
        if (bindingResult.hasErrors()) {
            StringBuffer stringBuffer = new StringBuffer();
            List<ObjectError> allErrors = bindingResult.getAllErrors();
            for (ObjectError objectError : allErrors) {
                stringBuffer.append(objectError.getDefaultMessage()).append("; ");
            }
            String s = stringBuffer.toString();
            System.out.println(s);
            return new Result<String>(false, StatusCode.ERROR, "修改失败",s);
        }
        questionService.updateById(question);
        return new Result(true,StatusCode.OK,"修改成功");
    }
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id")Integer id){
        questionService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }
}
